package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AUser;
import com.ruoyi.system.domain.vo.HgADataInfoVO;
import com.ruoyi.system.domain.vo.XcxADataInfoVO;
import com.ruoyi.system.mapper.AUserMapper;
import com.ruoyi.system.util.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ADataInfoMapper;
import com.ruoyi.system.domain.ADataInfo;
import com.ruoyi.system.service.IADataInfoService;

/**
 * 素材库详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
public class ADataInfoServiceImpl implements IADataInfoService {
    @Autowired
    private ADataInfoMapper aDataInfoMapper;

    @Autowired
    private AUserMapper aUserMapper;

    /**
     * 查询素材库详情
     *
     * @param id 素材库详情主键
     * @return 素材库详情
     */
    @Override
    public HgADataInfoVO selectADataInfoById(Long id) {
        return aDataInfoMapper.selectADataInfoById(id);
    }

    /**
     * 查询素材库详情列表
     *
     * @param aDataInfo 素材库详情
     * @return 素材库详情
     */
    @Override
    public List<HgADataInfoVO> selectADataInfoList(ADataInfo aDataInfo) {
        return aDataInfoMapper.selectADataInfoList(aDataInfo);
    }

    /**
     * 新增素材库详情
     *
     * @param aDataInfo 素材库详情
     * @return 结果
     */
    @Override
    public int insertADataInfo(ADataInfo aDataInfo) {
        aDataInfo.setCreateTime(DateUtils.getNowDate());
        return aDataInfoMapper.insertADataInfo(aDataInfo);
    }

    /**
     * 修改素材库详情
     *
     * @param aDataInfo 素材库详情
     * @return 结果
     */
    @Override
    public int updateADataInfo(ADataInfo aDataInfo) {
        aDataInfo.setUpdateTime(DateUtils.getNowDate());
        return aDataInfoMapper.updateADataInfo(aDataInfo);
    }

    /**
     * 批量删除素材库详情
     *
     * @param ids 需要删除的素材库详情主键
     * @return 结果
     */
    @Override
    public int deleteADataInfoByIds(Long[] ids) {
        return aDataInfoMapper.deleteADataInfoByIds(ids);
    }

    /**
     * 删除素材库详情信息
     *
     * @param id 素材库详情主键
     * @return 结果
     */
    @Override
    public int deleteADataInfoById(Long id) {
        return aDataInfoMapper.deleteADataInfoById(id);
    }

    @Override
    public List<ADataInfo> selectADataInfoXcxList(ADataInfo aDataInfo) {
        AUser au = LoginUtils.getUser();
        aDataInfo.setAgentLevel(au.getRole());
        List<ADataInfo> list = aDataInfoMapper.selectADataInfoXcxList(aDataInfo);
        if (!list.isEmpty()) {
            for (ADataInfo info : list) {
                info.setListImg(Arrays.asList(info.getDaCarousel().split(",")));
            }

        }
        return list;
    }

    @Override
    public XcxADataInfoVO selectADataInfoByInfo(XcxADataInfoVO xcxADataInfo) {
        xcxADataInfo.setUserId(LoginUtils.getUser().getId());
        XcxADataInfoVO vo = aDataInfoMapper.selectADataInfoByInfo(xcxADataInfo);
        if (vo != null && vo.getDaCarousel() != null) {
            String[] str = vo.getDaCarousel().split(",");
            List<String> arr = new ArrayList<String>();
            for (String s : str) {
                arr.add(s);
            }
            vo.setListImg(arr);
        }
        return vo;
    }
}
